#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from flask import Blueprint
import dbOperate

# db1 = dbOperate.dbOperate('192.168.1.3', 'zabbix', 'zabbix', 'zabbix')
db1 = dbOperate.dbOperate('10.37.133.21', 'spider', 'spider', 'spider')
database = Blueprint('database', __name__)

###创建一个host<>hostname映射字典
def getHostDict():
    dict = {}
    see = db1.exe_query('select host,hostname from hostDb')
    for data in see:  ####第一个see为字典
        dict[data[0]] = data[1]
    # print('hostdict----', len(dict))
    return dict

@database.route('/get_dbs_usage', methods=['GET'])
def get_dbs_usage():
    sql = 'select a.host,a.`usage` from tbsUsageTemp a'
    sql1 = 'SELECT `host`,tbsName,`usage` FROM tbsUsage as a, (SELECT max(id) as id from tbsUsage b GROUP BY ' \
           'tbsName) as b WHERE a.id = b.id'
    hostname = []
    value = []
    jsonData = {}
    dict = getHostDict()
    see = db1.exe_query(sql)
    for data in see:
        hostname.append(dict[data[0]])
        value.append(data[1])
    jsonData['hostname'] = hostname
    jsonData['value'] = value
    j = json.dumps(jsonData, ensure_ascii=False)
    print('dbbar--------' + str(len(j)))
    return j

@database.route('/get_dBCheck', methods=['GET'])
def get_dbCheck():
    sql = 'select a.host,a.instanceStatus,a.lsnrStatus,a.tbsUsage,a.tempTbsUsage from dBCheck a'
    hostname = []
    instanceStatus = []
    lsnrStatus = []
    tbsUsage = []
    tempTbsUsage = []
    jsonData = {}
    dict = getHostDict()
    see = db1.exe_query(sql)
    for data in see:
        hostname.append(dict[data[0]])
        instanceStatus.append(data[1])
        lsnrStatus.append(data[2])
        tbsUsage.append(data[3])
        tempTbsUsage.append(data[4])
    jsonData['hostname'] = hostname
    jsonData['instanceStatus'] = instanceStatus
    jsonData['lsnrStatus'] = lsnrStatus
    jsonData['tbsUsage'] = tbsUsage
    jsonData['tempTbsUsage'] = tempTbsUsage
    j = json.dumps(jsonData, ensure_ascii=False)
    print('db--------' + str(len(j)))
    return j